package com.cai.oamanager.controller;

import com.cai.oamanager.entity.Category;
import com.cai.oamanager.entity.People;
import com.cai.oamanager.entity.Student;
import com.cai.oamanager.service.CategoryService;
import com.cai.oamanager.service.MessageService;
import com.cai.oamanager.service.StudentService;
import com.cai.oamanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/10 19 : 55
 * @package：com.cai.oamanager.controller
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    @Autowired
    private StudentService studentService;

    /**
     * 登录
     * @param request
     * @param people
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, Model model,People people,String identity){
        System.out.println("identity:: "+identity);
        if(identity.equals("student")){
            request.getSession().setAttribute("identity",identity);
            Student student = studentService.login(people.getOname(), people.getOpassword());
            if(student==null){
                request.getSession().setAttribute("student",null);
                return "redirect:/router/login.html";
            }
            request.getSession().setAttribute("student",student);
            return "stumain";
        }
        People login = userService.login(people);
        if(login==null){
            request.getSession().setAttribute("people",null);
            return "redirect:/router/login.html";
        }
        System.out.println(login.toString());
        request.getSession().setAttribute("people",login);
//        int count = messageService.getNewReciveMessageCount(login.getOid(), login.getOposition());
//        model.addAttribute("count",count);
        //List<Category> categories = categoryService.allCategory(login.getOauth());
        //model.addAttribute("categories",categories);
        return "main";
    }


    /**
     * 人员管理
     学生管理
     教师管理
     院部管理
     院部管理
     文件管理
     发送文件
     接收文件
     消息管理
     发送消息
     接收消息
     权限管理
     权限管理
     * 注销
     */
    @RequestMapping("outLogin")
    public String outLogin(HttpServletRequest request){
        request.getSession().removeAttribute("people");
        request.getSession().removeAttribute("student");
        return "redirect:/router/login.html";
    }
}
